package com.nbsaas.boot.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

    public static String getMD5(String input) {
        try {
            // 创建一个MD5算法的MessageDigest实例
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 计算输入字符串的MD5哈希值
            byte[] messageDigest = md.digest(input.getBytes());

            // 将字节数组转换为十六进制格式的字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String input = "hello";
        String md5 = getMD5(input);
        System.out.println("MD5 hash of \"" + input + "\" is: " + md5);
    }
}
